package com.unisound.tracelog.client.core.support.async;

import com.unisound.tracelog.client.core.context.SpanContext;
import com.unisound.tracelog.client.core.context.SpanContextManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.Callable;

public class TraceLogCallableWrapper<V> implements Callable<V> {
    private static final Logger LOGGER = LoggerFactory.getLogger(TraceLogCallableWrapper.class);

    private Callable<V> task;
    private List<SpanContext> spanContexts;

    public TraceLogCallableWrapper(Callable<V> task) {
        this.task = task;
        this.spanContexts = SpanContextManager.getSpanContexts();
    }

    @Override
    public V call() throws Exception {
        try {
            SpanContextManager.update(spanContexts);
        } catch (Exception e) {
            LOGGER.error("call error.", e);
        } finally {
            return task.call();
        }
    }

}
